package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.resource.MessageConstant;
import com.itheima.service.CheckGruopService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGruopService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;


    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void addGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //先向t_checkgroup表中添加一条数据,需要返回记录生成的主键，提供联合主键使用
        checkGroupMapper.addGroup(checkGroup);

        //设置检查项和检查组的中间的表中添加数据
        addGroupAndCheckItem(checkitemIds,checkGroup.getId());
    }

    private  void addGroupAndCheckItem(Integer[] checkitemIds,Integer checkgruopId){
        //检查项的id数组不等于0时 循环的根据检查组的id和所选择的检查项的id 向中间表中添加
        if (checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkitemId:checkitemIds){
                checkGroupMapper.addGroupAndCheckItem(checkgruopId,checkitemId);
            }
        }
    }

    //分页查询
    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        Map map=new HashMap();
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList = checkGroupMapper.findPage(queryPageBean.getQueryString());
        PageInfo<CheckGroup> pageInfo=new PageInfo<CheckGroup>(checkGroupList);
        map.put("itemList",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }


    //根据检查组的id查询出对应检查项的id
    @Override
    public Integer[] findCheckItemIdsByCheckGroupId(Integer id) {
        Integer[] checkitemIds=checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        return checkitemIds;
    }

    //编辑检查组
    @Override
    public void editGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //先删除之前向t_checkitem_checkgroup添加的i
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //再向中间表添加新的id
        addGroupAndCheckItem(checkitemIds,checkGroup.getId());
        //编辑检查组
        checkGroupMapper.editGroup(checkGroup);

    }


    //删除检查组
    @Override
    public void deleteCheckGroupById(Integer id) {
        //先判断该检查组是否在套餐里
        int count = checkGroupMapper.findSetmealCheckgroup(id);
        if (count>0){ //检查组存在套餐中,不能删除
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }

        //不存在则可以删除,逻辑删除
        checkGroupMapper.deleteCheckGroupById(id);
    }

    //查询所有检查组
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList= checkGroupMapper.findAll();
        return checkGroupList;
    }
}
